﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
//using tuwien.auto.eibclient;
using System.Threading;
using System.Text.RegularExpressions;
using KNXFramework.KNXObjects;
using KNXFramework;
using ClientSample.KNXService;
using System.ServiceModel;

namespace ClientSample
{
    public partial class Form1 : Form//, KNXService.IObjectServiceCallback
    {

        private KNXObjectsManagerClient m_ObjectServiceClient;

        public Form1()
        {
            InitializeComponent();

        }
        Dictionary<string,KNXObject> list;
        private void button1_Click(object sender, EventArgs e)
        {
            string id = this.txtGA.Text;
            //KNXObject obj = m_ObjectServiceClient.ReadValue(this.txtGA.Text);
            //this.lblObjectValue.Text = obj.ObjectValue;
            list = m_ObjectServiceClient.GetAllObjects();
            
            this.dataGridView1.DataSource = list.Values.ToList<KNXObject>();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //InstanceContext context = new InstanceContext(this);
            m_ObjectServiceClient = new ClientSample.KNXService.KNXObjectsManagerClient();
            //WSDualHttpBinding _binding = m_ObjectServiceClient.Endpoint.Binding as WSDualHttpBinding;
            //_binding.ClientBaseAddress = new Uri(_binding.ClientBaseAddress.ToString() + Guid.NewGuid().ToString()); 
            m_ObjectServiceClient.StartConnection();            
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            m_ObjectServiceClient.CloseConnection();
            m_ObjectServiceClient.Close();
        }

        private void btnSwitch_Click(object sender, EventArgs e)
        {
            
            //Thread th = new Thread(new ThreadStart(Test));
            
            //th.Start();

            
            
            Dictionary<string, KNXObject> list = m_ObjectServiceClient.GetAllObjects();
            //this.dataGridView1.DataSource = list;
            //m_ObjectServiceClient.WriteValue("date", "now");
            //m_ObjectServiceClient.WriteValue("time", "now");
            
                       
        }
        private void Test()
        {
            string id = this.txtGA.Text;
            m_ObjectServiceClient.WriteValue(id, this.txtID.Text);
        }
        #region IObjectServiceCallback Members

        public void UpdateKNXObjectValue(string id, string newValue)
        {
            KNXObject obj = list[id];
            obj.ObjectValue = newValue;
            this.dataGridView1.DataSource = list.Values.ToList<KNXObject>();
        }

        #endregion

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show(m_ObjectServiceClient.ReadValue(this.txtID.Text).ObjectValue);
        }
    }
}
